package com.drama.api.company.company.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.Constants;
import com.drama.api.base.R;
import com.drama.api.company.company.domain.vo.CompanyLoginReqVO;
import com.drama.api.company.company.domain.vo.CompanyLoginResVO;
import com.drama.api.company.company.domain.vo.CompanyPasswordUpdateVO;
import com.drama.api.company.company.domain.vo.CompanyVO;
import com.drama.api.company.company.service.ICompanyService;
import com.drama.api.company.dramas.domain.vo.CompanyDramasCountSearchVO;
import com.drama.api.company.dramas.domain.vo.CompanyDramasCountVO;
import com.drama.api.company.dramas.service.ICompanyDramasService;
import com.drama.api.company.order.domain.vo.CompanyOrderCountSearchVO;
import com.drama.api.company.order.domain.vo.CompanyOrderCountVO;
import com.drama.api.company.order.service.ICompanyOrderService;
import com.drama.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.CompanyController)
@RequestMapping(value = "/api/company")
@RestController
public class CompanyController {

    @Autowired
    private ICompanyService service;

    @Autowired
    private ICompanyDramasService dramasService;

    @Autowired
    private ICompanyOrderService orderService;

    @ApiOperation(value = "商户登录", notes = "登录后取到接口得到token(13887888788/E10ADC3949BA59ABBE56E057F20F883E)")
    @PostMapping(value = "/login")
    @ResponseBody
    public R<CompanyLoginResVO> login(@ApiParam(value = "查询参数") @Valid @RequestBody CompanyLoginReqVO companyLogin, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            CompanyLoginResVO companyLoginResVO = service.login(companyLogin);
            result.setData(companyLoginResVO);
        }
        return result;
    }


    @ApiOperation(value = "修改当前登录用户密码")
    @PostMapping(value = "/update_password")
    @SaCheckLogin
    public R update_password(@ApiParam(value = "参数") @Valid @RequestBody CompanyPasswordUpdateVO passwordUpdateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.updatePassword(passwordUpdateVO);
        }
        return result;
    }


    @ApiOperation(value = "查询当前商户信息")
    @GetMapping(value = "/one")
    @SaCheckLogin
    public R<CompanyVO> one() {
        CompanyVO companyVO = service.getOne();
        return new R(companyVO);
    }

    @ApiOperation(value = "剧目情况概况")
    @GetMapping(value = "/drama_count")
    @SaCheckLogin
    public R<List<CompanyDramasCountVO>> drama_count() {
        CompanyDramasCountSearchVO searchVO=new CompanyDramasCountSearchVO();
        searchVO.setCompanyId(StpUtil.getLoginId().toString());
        List<CompanyDramasCountVO> list = dramasService.count(searchVO);
        return new R(list);
    }


    @ApiOperation(value = "收益概况")
    @GetMapping(value = "/order_count")
    public R<CompanyOrderCountVO> order_count(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyOrderCountSearchVO searchVO) {
        CompanyOrderCountVO countVO = orderService.getCount(searchVO);
        return new R(countVO);
    }

}
